package com.yq.tanhua.ui.recommend

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.navigation.fragment.findNavController
import coil.compose.AsyncImage
import com.bumptech.glide.Glide
import com.yq.tanhua.databinding.TanhuaFragmentRecommendBinding
import com.yq.tanhua.net.vo.TodayBest
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import com.yq.tanhua.R

class RecommendFragment : Fragment() {

    private var _binding: TanhuaFragmentRecommendBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    lateinit var recommendViewModel: RecommendViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        recommendViewModel =
            ViewModelProvider(this).get(RecommendViewModel::class.java)

        _binding = TanhuaFragmentRecommendBinding.inflate(inflater, container, false)
        val root: View = binding.root

        listenerData()
        recommendViewModel.initData()
        return root
    }

    private fun listenerData() {
        recommendViewModel.viewModelScope.launch {
            recommendViewModel.todayBestUiState.collectLatest {
                it.data?.let {
                    _binding?.tvName?.text = it.nickname
                    Glide.with(requireActivity()).load(it.avatar).into(binding.ivHead)

                }
                it.recomendList?.let {
                    updateRecommendList(it)
                }
            }
        }
    }

    private fun updateRecommendList(todayBests: List<TodayBest>) {
        binding.compose.setContent {
            LazyVerticalGrid(
                columns = GridCells.Fixed(2), content = {
                    itemsIndexed(todayBests) { index, item ->
                        Column(
                            modifier = Modifier
                                .padding(10.dp)
                                .border(0.2.dp, color = Color.Gray, shape = RectangleShape)
                                .clickable {
                                    jump2UserDetail(todayBests[index])
                                }
                        ) {
                            AsyncImage(
                                model = item.avatar,
                                contentScale = ContentScale.FillWidth,
                                modifier = Modifier
                                    .fillMaxWidth(1f)
                                    .height(150.dp),
                                contentDescription = "Translated description of what the image contains"
                            )
                            Row(modifier = Modifier.padding(vertical = 8.dp, horizontal = 4.dp)) {
                                Text(text = item.nickname)
                                Spacer(modifier = Modifier.weight(1f))
                                Text(text = item.age.toString())
                            }
                            Row(modifier = Modifier.padding(vertical = 8.dp, horizontal = 4.dp)) {
                                for (tag in item.tags) {
                                    Text(text = tag, modifier = Modifier.weight(1f))
                                }
                            }
                        }
                    }
                })
        }
    }

    private fun jump2UserDetail(todayBest: TodayBest) {
        val bundle = Bundle()
        bundle.putSerializable("todayBest", todayBest)
        findNavController().navigate(R.id.navigation_user_detail, bundle)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}